package nl.fonfolio.view {
	import flash.display.*;
	import flash.text.TextField;
	import flash.events.MouseEvent;

	import nl.fonfolio.model.Model;
	import nl.fonfolio.controller.Controller;

	import nl.fonfolio.core.FonfolioEvent;	
	import nl.fonfolio.core.Fonfolio;

	
	/**
	 * @author Niels
	 */
	public class Rating extends Sprite {
		private var model:Model;
		private var controller:Controller;
		
		public var rate_lbl:TextField;
		public var votes_txt:TextField;
		public var votes_lbl:TextField;

		private var voted : Boolean;
		public var makeVoting : MovieClip;
		public var currentRating : MovieClip;
		private var countVotes : Number;
		private var rating : Number;
		//public var ProjectRating : MovieClip;
		private var loggedOn : Boolean;
		private var fonfolio : Fonfolio;
		
		

		public function Rating(fonfolio:Fonfolio,model:Model,controller:Controller) {
			this.fonfolio = fonfolio;
			this.model = model;
			this.controller = controller;
			this.rating  = 2.5;
		
			this.model.addEventListener(FonfolioEvent.CONTROL_TYPE, this.update);
			
			var currentUser:Object = this.model.getCurrentUser();	
			//trace("currentuser=" + currentUser.userId);
			if (currentUser != null){
				if (currentUser.userId != null){
					this.loggedOn = true;
					makeVoting.addEventListener(MouseEvent.MOUSE_MOVE, setVoting);
					makeVoting.addEventListener(MouseEvent.CLICK, registerVoting);
					makeVoting.addEventListener(MouseEvent.MOUSE_OUT, resetVoting);
				}			
			} else this.loggedOn = false;
			this.voted = false;
			
			currentRating.masking.width = 25 * rating;
			makeVoting.masking.width = 0;
			
			this.updateCountVotes();

			
		
		}

		private function setVoting(event : MouseEvent) : void {
			if (!this.voted) {
				var mouse_x : Number = currentRating.unselected.mouseX;
				var vote : Number = Math.floor(mouse_x / 25) + 1;
				makeVoting.masking.width = 25 * vote;
			}
		}

		private function resetVoting(event : MouseEvent) : void {
			makeVoting.masking.width = 0;
		}

		private function registerVoting(event : MouseEvent) : void {
			if (!this.voted) {
				this.voted = true;
				makeVoting.masking.width = 0;
				var mouse_x : Number = currentRating.unselected.mouseX;
				var vote : Number = Math.floor(mouse_x / 25) + 1;
				trace("You voted: " + vote);
				this.countVotes++;
				this.updateCountVotes();
				var objRating:Object = new Object;
				objRating.projectId = 2;
				objRating.userId = 3;
				objRating.vote = vote;
				this.controller.insertVotes(objRating);
			}
		}

		private function updateCountVotes() :void{
			//ProjectRating.votes_txt.text = this.countVotes.toString();
		}

		private function update(event : FonfolioEvent) : void {
			
			switch (event.command){
				case "UPDATE_VOTES":
					trace("command = " + event.command);
					//this.rate_lbl.text = "boe";
					//this.votes_txt.text = "200";
					//this.votes_lbl.text = "bla";
							
				break;
			}
		}
	}
}

